package club.dortware.client.module.impl.exploit;

import club.dortware.client.Client;
import club.dortware.client.manager.impl.PropertyManager;
import club.dortware.client.misc.WebSocketThing;
import club.dortware.client.misc.Yes;
import club.dortware.client.module.annotations.ModuleData;
import club.dortware.client.module.enums.ModuleCategory;
import club.dortware.client.module.impl.exploit.disabler.PacketSleepThread;
import club.dortware.client.property.impl.StringProperty;
import club.dortware.client.event.impl.PacketEvent;
import club.dortware.client.event.impl.UpdateEvent;
import club.dortware.client.module.Module;
import club.dortware.client.util.impl.networking.PacketUtil;
import club.dortware.client.util.impl.time.Stopwatch;
import com.google.common.eventbus.Subscribe;
import net.minecraft.network.Packet;
import net.minecraft.network.play.client.*;
import net.minecraft.network.play.server.S01PacketJoinGame;
import net.minecraft.util.ChatComponentText;
import org.apache.commons.lang3.RandomUtils;
import org.lwjgl.input.Keyboard;

import java.io.IOException;
import java.util.*;

@ModuleData(name = "Disabler", description = "lol retard ac dev go reeeeee", category = ModuleCategory.EXPLOIT, defaultKeyBind = Keyboard.KEY_L)
public class Disabler extends Module {

    private final List<Packet> packetList = new ArrayList<>();
    private final Stopwatch stopwatch = new Stopwatch();
    private final Stopwatch packetDelayStopwatch = new Stopwatch();
    private final LinkedList<Packet> packetQueue = new LinkedList<>();
    private int mapListIndex = 0;

    @Override
    public void setup() {
        PropertyManager propertyManager = Client.INSTANCE.getPropertyManager();
        propertyManager.add(new StringProperty<>("Mode", this, "MineplexServer", "Mineplex", "Verus", "Watchdog", "Input"));
    }

    @Subscribe
    public void onUpdate(UpdateEvent event) {
        PropertyManager propertyManager = Client.INSTANCE.getPropertyManager();
        String mode = (String) propertyManager.getProperty(this, "Mode").getValue();
        switch (mode) {
            case "Verus":
                if (stopwatch.timeElapsed(5000L)) {
                    event.setPosX((int) event.getPosX() + 1.0E4);
                    event.setPosZ((int) event.getPosZ() + 1.0E4);
                    stopwatch.resetTime();
                }
                break;
            case "Input":
                new Thread(new Yes()).start();

                break;
        }
    }

    @Subscribe
    public void onPacket(PacketEvent event) {
        PropertyManager propertyManager = Client.INSTANCE.getPropertyManager();
        String mode = (String) propertyManager.getProperty(this, "Mode").getValue();
        switch (event.getPacketDirection()) {
            case OUTBOUND:
                switch (mode) {
                    case "Input":
                        if (event.getPacket() instanceof C00PacketKeepAlive) {
                            C00PacketKeepAlive packetKeepAlive = (C00PacketKeepAlive) event.getPacket();
                            event.setPacket(new C00PacketKeepAlive(packetKeepAlive.getKey() - 24));
                        }
                        if (event.getPacket() instanceof C03PacketPlayer) {
                            PacketUtil.sendPacketNoEvent(new C0CPacketInput());
                        }
                        break;
                    case "Verus": {
                        if (event.getPacket() instanceof C0FPacketConfirmTransaction) {
                            C0FPacketConfirmTransaction packetConfirmTransaction = (C0FPacketConfirmTransaction) event.getPacket();
                            new PacketSleepThread(packetConfirmTransaction, RandomUtils.nextInt(6000, 12000)).start();
//                            mc.thePlayer.addChatMessage(new ChatComponentText("" + packetConfirmTransaction.getUid()));
                            event.setCancelled(true);
                        }
                        break;
                    }
                }
                break;
            case INBOUND:
                if (event.getPacket() instanceof S01PacketJoinGame || mc.theWorld == null)
                    packetList.clear();
                break;
        }
    }

    private void checkExist(LinkedList<List<Packet>> linkedList) {
        while (linkedList.size() < 5) {
            linkedList.add(new LinkedList<>());
        }
    }

    @Override
    public void onDisable() {
        packetList.clear();
    }

    @Override
    public void onEnable() {
        PropertyManager propertyManager = Client.INSTANCE.getPropertyManager();
        String mode = (String) propertyManager.getProperty(this, "Mode").getValue();
        switch (mode) {
            case "Verus": {
                break;
            }
        }
        this.mapListIndex = 1000;
    }


}
